/*cardDoctorAdmin - controllers.js - Yadong Zhu 2014*/
(function() {
    'use strict';
    /* Controllers */
    angular.module('cardDoctorAdmin.controllers', [])
    .controller('baseCtrl', 
            ['$scope', '$routeParams','cardDoctorCacheService',
            function($scope, $routeParams,cardDoctorCacheService) {
                         //alerts
                $scope.debug = false;
                $scope.submitted = false;
                $scope.pageParts = {};
                $scope.pageParts.alerts = [];
                $scope.addAlert = function(type, text) {
                    return $scope.pageParts.alerts.push({
                        type: type,
                        msg: text
                    });
                };

                /**
                 * Function for closing alert.
                 * @param index
                 */
                $scope.closeAlert = function(index) {
                    $scope.pageParts.alerts.splice(index, 1);
                };

     
    }]) //end of baseCtrl
    .controller('searchCtrl', ['$scope', '$controller','$routeParams', '$location', 'cardDoctorAdminService', 'cardDoctorCacheService',
        function($scope,$controller, $routeParams, $location, cardDoctorAdminService, cardDoctorCacheService) {
        $controller("baseCtrl", {
            $scope: $scope,
            $routeParams:$routeParams,
            cardDoctorCacheService:cardDoctorCacheService
        });
            // var lastSearch = '';
            // $scope.currentPage = 1;
            // $scope.searchResults = cardDoctorCacheService.search({pg:$scope.currentPage});

            // $scope.search = function search() {
            //     $scope.currentSearch = $scope.searchTerm;
            //     $location.search('search', $scope.currentSearch);

            //     if ($scope.currentSearch && $scope.currentSearch.length > 0) {
            //         //see if we have a new search term, if so reset pages.
            //         if (!lastSearch.localeCompare($scope.currentSearch)) {
            //             lastSearch = $scope.currentSearch;
            //             $scope.currentPage = 0;
            //         }
            //         $scope.searchResults = cardDoctorCacheService.search({
            //             pg:$scope.currentPage,
            //             's': $scope.currentSearch
            //         });
            // }

            // };
            // $scope.location = $location;
            // if ($location.search) {
            //     $scope.searchTerm = ($location.search()).search;
            //     $scope.search();
            // }
            //  $scope.pageChanged = function() {
            //     // console.log('Page changed to: ' + $scope.currentPage);
            //     cardDoctorCacheService.search({pg:$scope.currentPage}).$promise.then(function(data){
            //         $scope.searchResults=data;
            //     });
            //   };
        }
    ])
        .controller('cardAdminCtrl', ['$scope','$controller', '$routeParams', 'cardDoctorCacheService', 'card',
            function($scope,$controller, $routeParams, cardDoctorCacheService, card) {
                $controller("baseCtrl", {
                    $scope: $scope,
                    $routeParams:$routeParams,
                    cardDoctorCacheService:cardDoctorCacheService
                });
                $scope.user = {username:"Yadong"}
        	}
               
        ]) //end of controller bracketAdminController
 
        .controller('chatCtrl', ['$scope','$controller', '$routeParams', 'cardDoctorCacheService',
                                            function($scope,$controller, $routeParams, cardDoctorCacheService) {
        	$controller("baseCtrl", {
        		$scope: $scope,
        		$routeParams:$routeParams,
        		cardDoctorCacheService:cardDoctorCacheService
        	});
            $scope.user = {username:"Yadong"}
        }
        
        ]) //end of controller bracketAdminController
        
        .controller('meCtrl', ['$scope','$controller', '$routeParams', 'cardDoctorCacheService', 
                                            function($scope,$controller, $routeParams, cardDoctorCacheService) {
        	$controller("baseCtrl", {
        		$scope: $scope,
        		$routeParams:$routeParams,
        		cardDoctorCacheService:cardDoctorCacheService
        	});
        }
        
        ]) //end of controller bracketAdminController
        
        .controller('discoverCtrl', ['$scope','$controller', '$routeParams', 'cardDoctorCacheService',
                                            function($scope,$controller, $routeParams, cardDoctorCacheService) {
        	$controller("baseCtrl", {
        		$scope: $scope,
        		$routeParams:$routeParams,
        		cardDoctorCacheService:cardDoctorCacheService
        	});
        }
        
        ]) //end of controller bracketAdminController
        
        .controller('signupCtrl', ['$scope','$controller', '$routeParams', 'cardDoctorCacheService',
                                            function($scope,$controller, $routeParams, cardDoctorCacheService) {
        	$controller("baseCtrl", {
        		$scope: $scope,
        		$routeParams:$routeParams,
        		cardDoctorCacheService:cardDoctorCacheService
        	});
        }
        
        ]) //end of controller bracketAdminController
        
        .controller('signinCtrl', ['$scope','$controller', '$routeParams', 'cardDoctorCacheService',
                                            function($scope,$controller, $routeParams, cardDoctorCacheService) {
        	$controller("baseCtrl", {
        		$scope: $scope,
        		$routeParams:$routeParams,
        		cardDoctorCacheService:cardDoctorCacheService
        	});
        }
        
        ]) //end of controller bracketAdminController
        
        

	.controller("loginCtrl",[ "$scope", "$modalInstance", '$route', "loginService",
					function($scope, $modalInstance, $route, loginService) {
						$scope.close = function() {
							$modalInstance.dismiss('close');
						};
						$scope.login = {
							username : "",
							password : ""
						};
						$scope.submitForm = function() {
							// console.log("submit login
							// form:"+$scope.login.username+":"+$scope.login.password);
							loginService.login({
								action : "login"
							}, $scope.login).$promise.then(function(data) {
								console.log("login callback:" + data.status);
								$modalInstance.dismiss('close');
								$route.reload();
								// $location.path($location.path);
							}, function(err) {
								console.error("login fail:" + err.data.status);
								// console.error("login error
								// message:"+err.data.errorMessage);
								$scope.login.errMsg = err.data.status;

							})
						}
					} 
		])
        .controller("ModalInstanceCtrl", ["$scope", "$modalInstance",
            function($scope, $modalInstance) {
                $scope.close = function() {
                    $modalInstance.dismiss('close');
                };
            }
        ])
        .controller("DropdownCtrl", ['$scope',
            function($scope) {
                //just to pass scope into the dropdown
            }
        ]);
}());
